package com.dongdongshop.controller;

import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.evaluate.Evaluate;
import com.dongdongshop.service.evaluate.EvaluateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/4/3 21:14
 */
@RestController
@RequestMapping("evaluate")
public class EvaluateController {

    private static final Logger log = LoggerFactory.getLogger(EvaluateController.class);

    @Autowired
    private EvaluateService evaluateService;


    /**
     * 增加评论
     * @param evaluate
     * @return
     */
    @PostMapping("/add")
    public DataResult addEvaluate(@RequestBody Evaluate evaluate){
        try {
            evaluateService.addEvaluate(evaluate);
        }catch (Exception e){
            log.error("增加评论异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 通过goodsid查询所有评论
     * @return
     */
    @GetMapping("/{goodsId}")
    public DataResult<List<Evaluate>> findByGoodsId(@PathVariable("goodsId") Long goodsId){
        List<Evaluate> evaluates = null;
        try {
            evaluates = evaluateService.findByGoodsId(goodsId);
        }catch (Exception e){
            log.error("评论查询异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),evaluates);
    }

    /**
     * 评论点赞
     * @param id
     */
    @PostMapping("/{id}")
    public DataResult incLikeNum(@PathVariable("id") Integer id){
        try {
            evaluateService.incLikeNum(id);
        }catch (Exception e){
            log.error("点赞评论异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
}
